package com.ruoyi.consumer.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.consumer.domain.CCompany;
import com.ruoyi.consumer.domain.CInvoiceHeader;
import com.ruoyi.consumer.service.ICCompanyService;
import com.ruoyi.consumer.service.ICInvoiceHeaderService;
import com.ruoyi.system.service.ISysUserService;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 发票抬头Controller
 * 
 * @author lizhouwei
 * @date 2023-12-09
 */
@RestController
@RequestMapping("/consumer/header")
public class CInvoiceHeaderController extends BaseController
{
    @Autowired
    private ICInvoiceHeaderService cInvoiceHeaderService;

    @Autowired
    private ICCompanyService cCompanyService;

    @Autowired
    private ISysUserService sysUserService;


    /**
     * 查询发票抬头列表
     */
    @PreAuthorize("@ss.hasPermi('consumer:header:list')")
    @GetMapping("/list")
    public TableDataInfo list(CInvoiceHeader cInvoiceHeader)
    {
        Long userAdminId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userAdminId);
        Assert.notNull(sysUser, "用户不存在");


        if (userAdminId != 1) {
            CCompany company = new CCompany();
            company.setCompanDept(Math.toIntExact(sysUser.getDeptId()));
            List<CCompany> cCompany = cCompanyService.selectCCompanyList(company);
            if (cCompany.isEmpty()) {
                return null;
            }else {
                cInvoiceHeader.setCompanyId(cCompany.get(0).getCompanyId());
            }

        }
        startPage();
        List<CInvoiceHeader> list = cInvoiceHeaderService.selectCInvoiceHeaderList(cInvoiceHeader);
        return getDataTable(list);
    }

    /**
     * 导出发票抬头列表
     */
    @PreAuthorize("@ss.hasPermi('consumer:header:export')")
    @Log(title = "发票抬头", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CInvoiceHeader cInvoiceHeader)
    {
        List<CInvoiceHeader> list = cInvoiceHeaderService.selectCInvoiceHeaderList(cInvoiceHeader);
        ExcelUtil<CInvoiceHeader> util = new ExcelUtil<CInvoiceHeader>(CInvoiceHeader.class);
        util.exportExcel(response, list, "发票抬头数据");
    }

    /**
     * 获取发票抬头详细信息
     */
    @PreAuthorize("@ss.hasPermi('consumer:header:query')")
    @GetMapping(value = "/{invoiceId}")
    public AjaxResult getInfo(@PathVariable("invoiceId") Integer invoiceId)
    {
        return success(cInvoiceHeaderService.selectCInvoiceHeaderByInvoiceId(invoiceId));
    }

    /**
     * 新增发票抬头
     */
    @PreAuthorize("@ss.hasPermi('consumer:header:add')")
    @Log(title = "发票抬头", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CInvoiceHeader cInvoiceHeader)
    {
        Long userAdminId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userAdminId);
        Assert.notNull(sysUser, "用户不存在");


        CCompany company = new CCompany();
        company.setCompanDept(Math.toIntExact(sysUser.getDeptId()));
        List<CCompany> cCompany = cCompanyService.selectCCompanyList(company);
        if (cCompany.isEmpty()) {
            return null;
        }else {
            cInvoiceHeader.setCompanyId(cCompany.get(0).getCompanyId());
        }
        return toAjax(cInvoiceHeaderService.insertCInvoiceHeader(cInvoiceHeader));
    }

    /**
     * 修改发票抬头
     */
    @PreAuthorize("@ss.hasPermi('consumer:header:edit')")
    @Log(title = "发票抬头", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CInvoiceHeader cInvoiceHeader)
    {
        return toAjax(cInvoiceHeaderService.updateCInvoiceHeader(cInvoiceHeader));
    }

    /**
     * 删除发票抬头
     */
    @PreAuthorize("@ss.hasPermi('consumer:header:remove')")
    @Log(title = "发票抬头", businessType = BusinessType.DELETE)
	@DeleteMapping("/{invoiceIds}")
    public AjaxResult remove(@PathVariable Integer[] invoiceIds)
    {
        return toAjax(cInvoiceHeaderService.deleteCInvoiceHeaderByInvoiceIds(invoiceIds));
    }
}
